<?php
/**
 * FastApp.
 * 2/2/25
 * @Author deepsea
 * @Contact (telegram:deepsea159)
 */

namespace App\Game\Command;

use App\Fastapp\Model\FastappCategory;
use App\Game\Api\Service\GameService;
use App\Game\Model\Game;
use App\Game\Model\GameCategory;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;

#[Command]
class GameCommand extends HyperfCommand
{
    public function __construct(protected ContainerInterface $container)
    {
        parent::__construct('game:init');
    }

    public function configure(): void
    {
        parent::configure();
        $this->setDescription('初始化游戏数据');
    }

    public function handle(): void
    {
        GameCategory::query()->truncate();
        $in = [];
        foreach (Game::query()->whereIn('type', [2, 7])->pluck('id')->toArray() as $id) {
            $in[] = [
                'game_id' => $id,
                'category_id' => 11,
            ];
        }
        foreach (Game::query()->whereIn('type', [1, 4, 5, 6])->pluck('id')->toArray() as $id) {
            $in[] = [
                'game_id' => $id,
                'category_id' => 13,
            ];
        }
        if ($in) GameCategory::query()->insert($in);
        $service = new GameService();
        $res = $service->gameNum();
        $category = FastappCategory::query()->whereIn('code', array_keys($res))->pluck('id', 'code')->toArray();
        $in = [];
        foreach ($res as $k => $v) {
            if (!isset($category[$k])) continue;
            foreach ($v['ids'] as $game_id) {
                $in[] = [
                    'game_id' => $game_id,
                    'category_id' => $category[$k],
                ];
            }
        }
        if ($in) GameCategory::query()->insert($in);
    }

}